// #include "stdio.h"
#include "caipu.h"
// #include "lj.h"
#include "string.h"
#include "lj_log.h"

#define _TAG_ "caipu"
#define log_zzc_n(fmt,...)  \
	do{\
		lj_set_log_head(0);\
		LJ_LOGI(_TAG_,fmt,##__VA_ARGS__);\
		lj_set_log_head(1);\
	}while(0)

#define log_zzc(fmt,...) LJ_LOGI(_TAG_,fmt,##__VA_ARGS__)

#define log_zzc_error(fmt,...)  LJ_LOGE(_TAG_,fmt,##__VA_ARGS__)


caipu_send_buf_fun_t caipu_send_buf_fun;

unsigned char stack_judge_buf[stack_MAX]={0};
unsigned char stack_value_buf[stack_MAX]={0};
unsigned char stack_count=0;
unsigned int judge_delay_time=0;//
unsigned char caipu_code[4]={0};
unsigned char caipu_buzhou_num=0;
unsigned char caipu_version=1;
unsigned char caipu_buzhou_show=0;

unsigned char caipu_head[6]={0};
unsigned char caipu_buzhou_now=0;
unsigned char caipu_step=0;
unsigned short caipu_timer_delay=0;
unsigned char need_end_caipu=0;

static unsigned char caipu_heartbeat_timeout=0;
static unsigned char caipu_heartbeat_time=0;

// unsigned char lj_push_mode=LJ_PUSH_MODE_ORI;
// unsigned char lj_setting[POINT_ALL_NUM_MAX+1]={0};
// int lj_setting_len=0;
// unsigned int lj_time_can_push=0;

//unsigned char dev_kind=K_DIANCILU;

unsigned char get_ack=0;
//unsigned char liandong_gonglv=0;
unsigned char caipu_change=0;


void print_point(void);
unsigned char  point_find_int_num(unsigned char index);
unsigned char  point_find_char_num(unsigned char index);
unsigned char caipu_is_running(void);

///////////////////////////////////////////////
//unsigned char power=0;
//unsigned char gonglv=0;
//unsigned char temp_set=0;
//unsigned char mode=0;
//unsigned char temp_now=0;
//unsigned short yuyue=0;;
//unsigned char dingshi=0;
//unsigned char tongsuo=0;
//unsigned char error_code=0;
//unsigned char guo_dangwei=0;
//unsigned char guo_dingshi=0;
//unsigned char guo_dianliang=0;


//typedef struct {
//    unsigned char index;
//    unsigned char kind;
//    unsigned char len;
//    void *value;
//}point_st_st;
//
//point_st_st point[]=
//{
//    0x00,0x00,0x01,&power,
//    0x01,0x00,0x01,&gonglv,
//    0x02,0x00,0x01,&temp_set,
//    0x03,0x00,0x01,&mode,
//    0x04,0x00,0x01,&dingshi,
//    0x05,0x00,0x01,&tongsuo,
//    0x06,0x20,0x02,&yuyue,
//    0x07,0x00,0x01,&temp_now,
//
//    0x10,0x00,0x01,&error_code,
//		0x0f,0x00,0x01,&dev_kind,
//
//	0x21,0x00,0x01,&guo_dangwei,
//	0x22,0x00,0x01,&guo_dingshi,
//	0x23,0x00,0x01,&guo_dianliang,
//
//	P_TIMER,0x20,0x02,&caipu_timer_delay,
//	P_STEP,0x00,0x01,&caipu_buzhou_now,
//	0x64,0x90,0x05,&caipu_head,
//};





point_int_st point_int[POINT_INT_NUM_MAX]={
	{1,1,0,1,0,0,},
	{1,2,0x20,2,0,0,},
	{1,3,0,1,0,0,},
	{1,4,0x40,04,0,0,},
	{1,P_TIMER,0x20,0x02,0,0,},
	{1,0x10,0,1,0,0,},
	{1,7,0,1,0,0,},
	{1,8,0,1,0,0,},
	{1,0,0,1,0,0,},
	{1,9,0,1,0,0,},
	{0,0,0,0,0,0,},
	{0,0,0,0,0,0,},
	{0,0,0,0,0,0,},
	{0,0,0,0,0,0,},
	{0,0,0,0,0,0,},
	{0,0,0,0,0,0,},
	{0,0,0,0,0,0,},
	{0,0,0,0,0,0,},
	{1,U_BEGIN,0,1,0,0,},
	{1,U_HEARTBEAT,0,1,0,0,},
};

// unsigned char change_index_buf[POINT_ALL_NUM_MAX]={0};




//unsigned char point_char_buf[POINT_CHAR_LEN_MAX]={0};
//unsigned char point_char_buf_index[POINT_CHAR_NUM_MAX]={0,1,2};
point_char_st point_char[POINT_CHAR_NUM_MAX]={
	{1,5,0xa0,24,"123",0,},
	{1,6,0x90,12,"223456789012",0,},
	{0,8,0x90,12,"323456789012",0,},
};


void reset_change(void)
{
	unsigned char i=0;
	for(i=0;i<POINT_INT_NUM_MAX;i++)
	{
		point_int[i].change=0;
	}
	for(i=0;i<POINT_CHAR_NUM_MAX;i++)
	{
		point_char[i].change=0;
	}

}

unsigned char check_change(unsigned char *unchack,unsigned char len)
{
	unsigned char i=0,j=0;
	for(i=0;i<POINT_INT_NUM_MAX;i++)
	{
		if(point_int[i].change==1)
		{
			for(j=0;j<len;j++)
			{
				if(point_int[i].index==unchack[j])
				{
					break;
				}
			}
			if(j>=len)
			{
				return 1;
			}
		}
	}
	for(i=0;i<POINT_CHAR_NUM_MAX;i++)
	{
		if(point_char[i].change==1)
		{
			for(j=0;j<len;j++)
			{
				if(point_char[i].index==unchack[j])
				{
					break;
				}
			}
			if(j>=len)
			{
				return 1;
			}
		}
	}
	return 0;
}

#ifdef _dianfanbao_
unsigned  char dfb_mode_old=0;
#endif //_dianfanbao_


unsigned char  caipu_deal_85(unsigned char *buf,unsigned short len)
{
	unsigned short i=0;
	unsigned char index=0,kind=0;
	unsigned short point_len=0;
	unsigned char ret=1;
	for(i=0;i<len;)
	{
		index=buf[i];
		kind=(buf[i+1]&0xf0);
		point_len=(buf[i+1]&0x0f)*256+buf[i+2];
		switch(kind)
		{
			case 0x00:
			case 0x10:
			case 0x20:
			case 0x30:
			case 0x40:
			case 0x70:
			case 0x80:
			case 0xc0:
				set_point_int_value(index,kind,point_len,&buf[i+3]);
#ifdef _CAIPU_STD_
				if(index==U_BEGIN)
				{
					if((buf[i+3]==0x00)&&(caipu_is_running()))
					{
						need_end_caipu=1;
					}
					ret= 0;
				}
				if(index==U_HEARTBEAT)
				{
					if(buf[i+3]==1)
					{
						caipu_heartbeat_timeout=0;
					}
					ret =0;
				}
#endif //_CAIPU_STD_
				break;
			case 0x90:
			case 0xa0:
				set_point_char_value(index,kind,point_len,&buf[i+3]);
			break;
		}
		// ret=point_find_int_num(index);
		// if(ret==0xff)
		// {

		// }
		// else{
		// 	set_point_int_value(index,kind,point_len,&buf[i+3]);
		// }
		// ret=point_find_char_num(index);
		// if(ret==0xff)
		// {

		// }
		// else{
		// 	set_point_char_value(index,kind,point_len,&buf[i+3]);
		// }
		i=i+point_len+3;
	}
	print_point();
#ifdef _dianfanbao_
	if(((point_int[0].value==0x00)||(point_int[8].value==0x00))&&(dfb_mode_old==0x08))
	{
		need_end_caipu=1;
	}
	dfb_mode_old=point_int[0].value;
#endif //_dianfanbao_
	return ret;
}


#define NUM_86 (7)
unsigned char index_86[NUM_86]={1,2,3,4,7,8,0x10};

void caipu_deal_86(unsigned char *buf,unsigned short len)
{
	unsigned short i=0;
	unsigned char index=0,kind=0;
	unsigned short point_len=0;
	unsigned char ret=0;
	unsigned short j=0;
	for(i=0;i<NUM_86;i++)
	{
		index=index_86[i];
		ret=point_find_int_num(index);
		if(ret==0xff)
		{

		}
		else{
			kind=point_int[ret].kind;
			point_len=point_int[ret].len;
			set_point_int_value(index,kind,point_len,&buf[j]);
			j=j+point_len;
		}
		ret=point_find_char_num(index);
		if(ret==0xff)
		{

		}
		else{
			kind=point_int[ret].kind;
			point_len=point_int[ret].len;
			set_point_char_value(index,kind,point_len,&buf[j]);
			j=j+point_len;
		}
	}
	print_point();
#ifdef _dianfanbao_
	if(((point_int[0].value==0x00)||(point_int[8].value==0x00))&&(dfb_mode_old==0x08))
	{
		need_end_caipu=1;
	}
	dfb_mode_old=point_int[0].value;
#endif //_dianfanbao_
}


void empty_point_int(void)
{
	unsigned char i=0;
	for(i=0;i<POINT_INT_NUM_MAX;i++)
	{
		point_int[i].full=0;
	}
}


unsigned char get_point_int(unsigned char index,unsigned int *out)
{
	if(point_find_int_num(index)!=0xff)
	{
		*out= point_int[index].value;
		return 1;
	}
	return 0;
}


void set_point_int_value(unsigned char index,unsigned char kind,unsigned char len,unsigned char *buf)
{
    unsigned  char i=0;//,j=0;
    unsigned int value_old=0;
    // unsigned char had_change=0;
    for(i=0;i<POINT_INT_NUM_MAX;i++)
    {
    	if(point_int[i].full)
    	{
    		if(point_int[i].index==index)
    		{
    			point_int[i].kind=kind;
    			point_int[i].len=len;

    			value_old=point_int[i].value;
    			switch(kind)
				{
					case C_LJ_DP_BYTE:
					case C_LJ_DP_INT8:
						point_int[i].value=buf[0];
					break;
					case C_LJ_DP_INT16:
					case C_LJ_DP_UINT16:
						point_int[i].value=buf[0]*256+buf[1];
					break;
					case C_LJ_DP_INT32:
					case C_LJ_DP_UINT32:
						point_int[i].value=buf[0]*256*256*256+buf[1]*256*256+buf[2]*256+buf[3];
					break;
					case C_LJ_DP_FLOAT:
					case C_LJ_DP_DOUBLE:
						point_int[i].value=buf[0]*256*256*256+buf[1]*256*256+buf[2]*256+buf[3];
					break;
//					case C_LJ_DP_STRING:
//					case C_LJ_DP_BINARY:
//					for(j=0;j<point[i].len;j++)
//					{
//						*((unsigned char*)point[i].value+j)=buf[j];
//					}
//					break;
					default:
					break;
				}
    			if(point_int[i].value!=value_old)
    			{
    				point_int[i].change=1;
    			}
				else{
					point_int[i].change=0;
				}
    			break;
    		}
    	}
    	else
    	{
    		// had_change=1;
			point_int[i].change=1;
    		point_int[i].index=index;
    		point_int[i].kind=kind;
    		point_int[i].full=1;
    		switch(kind)
			{
				case C_LJ_DP_BYTE:
				case C_LJ_DP_INT8:
					point_int[i].value=buf[0];
				break;
				case C_LJ_DP_INT16:
				case C_LJ_DP_UINT16:
					point_int[i].value=buf[0]*256+buf[1];
				break;
				case C_LJ_DP_INT32:
				case C_LJ_DP_UINT32:
					point_int[i].value=buf[0]*256*256*256+buf[1]*256*256+buf[2]*256+buf[3];
				break;
				case C_LJ_DP_FLOAT:
				case C_LJ_DP_DOUBLE:
					point_int[i].value=buf[0]*256*256*256+buf[1]*256*256+buf[2]*256+buf[3];
				break;
				default:
					point_int[i].index=0;
					point_int[i].kind=0;
					point_int[i].full=0;
					point_int[i].change=0;
				break;
			}
    		break;
    	}
    }
    if(i>=POINT_INT_NUM_MAX)
    {
    	log_zzc("point int is full\r\n");
    }
    // return had_change;
	// print_point();
}

void set_point_char_value(unsigned char index,unsigned char kind,unsigned short len,unsigned char *buf)
{
    unsigned  char i=0,j=0;
	// unsigned char had_change=0;
    for(i=0;i<POINT_CHAR_NUM_MAX;i++)
    {
    	if(point_char[i].full)
    	{
    		if(point_char[i].index==index)
    		{
    			point_char[i].kind=kind;
    			point_char[i].len=len;
    			switch(kind)
				{
					case C_LJ_DP_STRING:
					case C_LJ_DP_BINARY:
					point_char[i].change=0;
					for(j=0;j<len;j++)
					{
						if(point_char[i].value[j]!=buf[j])
						{
							point_char[i].value[j]=buf[j];
							point_char[i].change=1;
						}
					}
					break;
					default:
					break;
				}
    			break;
    		}
    	}
    	else
    	{
			point_char[i].change=1;
    		point_char[i].index=index;
    		point_char[i].kind=kind;
    		point_char[i].len=len;
    		point_char[i].full=1;
    		switch(kind)
			{
    			case C_LJ_DP_STRING:
				case C_LJ_DP_BINARY:
				for(j=0;j<point_char[i].len;j++)
				{
					point_char[i].value[j]=buf[j];
				}
				break;
				default:
					point_char[i].index=0;
					point_char[i].kind=0;
					point_char[i].len=0;
					point_char[i].full=0;
					point_char[i].change=0;
				break;
			}
			// had_change=1;
    		break;
    	}
    }
	if(i>=POINT_CHAR_NUM_MAX)
	{
		log_zzc("point char is full\r\n");
	}
	// return had_change;
}


void print_point(void)
{
    unsigned char i=0,j=0;
	// log_zzc("");
    for(i=0;i<POINT_INT_NUM_MAX;i++)
    {
		
        log_zzc_n("full=%x  %02x,%02x,%02x,change=%d = ",point_int[i].full,point_int[i].index,point_int[i].kind,point_int[i].len,point_int[i].change);
        for(j=0;j<4;j++)
        {
            log_zzc_n(" %02x",*((unsigned char *)(&point_int[i].value)+j));
        }
        log_zzc_n("\r\n");
    }

    for(i=0;i<POINT_CHAR_NUM_MAX;i++)
	{
		// log_zzc("");
		log_zzc_n("full=%x  %02x,%02x,%04x change=%d = ",point_char[i].full,point_char[i].index,point_char[i].kind,point_char[i].len,point_char[i].change);
		for(j=0;j<point_char[i].len;j++)
		{
			log_zzc_n(" %02x",*((unsigned char *)(&point_char[i].value[j])));
		}
		log_zzc_n("\r\n");
	}
}

unsigned char  point_find_int_num(unsigned char index)
{
    unsigned char i=0xff;
    for(i=0;i<POINT_INT_NUM_MAX;i++)
    {
        if((point_int[i].index==index)&&(point_int[i].full))
        {
            return i;
        }
    }
    return 0xff;
}

unsigned char  point_find_char_num(unsigned char index)
{
    unsigned char i=0xff;
    for(i=0;i<POINT_CHAR_NUM_MAX;i++)
    {
        if((point_char[i].index==index)&&(point_char[i].full))
        {
            return i;
        }
    }
    return 0xff;
}


/*
buf :buf need to check
len_out: out put len of point whose index is buf[0]
return :F_TRUE F_FASLE F_WRONG
*/



#define FLOAT_DIV (0.01)
unsigned char  get_true_false(unsigned char *buf,unsigned short *len_out)
{
	unsigned char index=0xff;//point_find_num(buf[0]);
    unsigned char maths=buf[1];
    unsigned short len=0;
    unsigned char i=0;
    unsigned short j=0;
    unsigned char kind=0;
    unsigned int value=0;
    unsigned int value_buf=0;
	int value_i=0;
	float value_f=0;
	float value_buf_f=0;
	int value_buf_i=0;
	unsigned char float_buf[4]={0};

	index=point_find_int_num(buf[0]);
	if(index==0xff)
	{
		// log_zzc("get_true_false index not int index=%02x\r\n",buf[0]);
	}
	else
	{
		if((maths<F_EQUAL)||(maths>F_RIGHT))
		{
			log_zzc("wrong7\r\n");
			return F_WRONG;
		}
		len=point_int[index].len;
		*len_out=len;
		kind=point_int[index].kind;
		switch(kind)
		{
			case C_LJ_DP_BYTE:
				// value_buf=*(unsigned char*)(&point_int[index].value);
				value_buf=point_int[index].value;
				value=buf[i+2];
			break;
			case C_LJ_DP_UINT16:
				value_buf=point_int[index].value;
				// value_buf=*(unsigned short*)(&point_int[index].value);
//				value=*(unsigned short*)(&buf[i+2]);
				value=buf[i+2]*256+buf[i+3];
			break;
			case C_LJ_DP_UINT32:
				value_buf=point_int[index].value;
				// value_buf=*(unsigned int*)point_int[index].value;
//				value=*(unsigned int*)(&buf[i+2]);
				value=buf[i+2]*256*256*256+buf[i+3]*256*256+buf[i+4]*256+buf[i+5];
			break;
			case C_LJ_DP_INT8:
				value_buf_i=point_int[index].value;
				// value_buf_i=*(char*)point_int[index].value;
				value_i=*(char*)(&buf[i+2]);
			break;
			case C_LJ_DP_INT16:
				value_buf_i=point_int[index].value;
				// value_buf_i=*(short*)point_int[index].value;
//				value_i=*(short*)(&buf[i+2]);
				value=buf[i+2]*256+buf[i+3];
			break;
			case C_LJ_DP_INT32:
				value_buf_i=point_int[index].value;
				// value_buf_i=*(int*)point_int[index].value;
//				value_i=*(int*)(&buf[i+2]);
				value=buf[i+2]*256*256*256+buf[i+3]*256*256+buf[i+4]*256+buf[i+5];
			break;
			case C_LJ_DP_FLOAT:
//			case C_LJ_DP_DOUBLE:
				value_buf_f=*(float*)(&point_int[index].value);
//				value_f=*(float*)(&buf[i+2]);
				float_buf[0]=buf[i+5];
				float_buf[1]=buf[i+4];
				float_buf[2]=buf[i+3];
				float_buf[3]=buf[i+2];
				value_f=*(float*)float_buf;
			break;
//			case 0x90:
//				break;
			default:
				break;
		}
		switch(maths)
		{
			case F_BIGGER:
				switch(kind)
				{
					case C_LJ_DP_BYTE:
					case C_LJ_DP_UINT16:
					case C_LJ_DP_UINT32:
						if(value_buf>value)
						{
								return F_TRUE;
						}
						else{
								return F_FALSE;
						}
//								break;
					case C_LJ_DP_INT8:
					case C_LJ_DP_INT16:
					case C_LJ_DP_INT32:
						if(value_buf_i>value_i)
						{
								return F_TRUE;
						}
						else{
								return F_FALSE;
						}
//								break;
					case C_LJ_DP_FLOAT:
						if(value_buf_f>value_f)
						{
							if(value_buf_f-value_f>FLOAT_DIV)
							{
								return F_TRUE;
							}
						}
						return F_FALSE;
//								break;
				}
			break;
			case F_SMALLER:
			switch(kind)
				{
					case C_LJ_DP_BYTE:
					case C_LJ_DP_UINT16:
					case C_LJ_DP_UINT32:
						if(value_buf<value)
						{
								return F_TRUE;
						}
						else{
								return F_FALSE;
						}
//								break;
					case C_LJ_DP_INT8:
					case C_LJ_DP_INT16:
					case C_LJ_DP_INT32:
						if(value_buf_i<value_i)
						{
								return F_TRUE;
						}
						else{
								return F_FALSE;
						}
//								break;
					case C_LJ_DP_FLOAT:
						if(value_buf_f<value_f)
						{
							if(value_f-value_buf_f>FLOAT_DIV)
							{
								return F_TRUE;
							}
						}
						return F_FALSE;
//								break;
				}
			break;
			case F_EQUAL:
			switch(kind)
				{
					case C_LJ_DP_BYTE:
					case C_LJ_DP_UINT16:
					case C_LJ_DP_UINT32:
						if(value_buf==value)
						{
								return F_TRUE;
						}
						else{
								return F_FALSE;
						}
//								break;
					case C_LJ_DP_INT8:
					case C_LJ_DP_INT16:
					case C_LJ_DP_INT32:
						if(value_buf_i==value_i)
						{
								return F_TRUE;
						}
						else{
								return F_FALSE;
						}
//								break;
					case C_LJ_DP_FLOAT:
						if(value_buf_f>=value_f)
						{
							if(value_buf_f-value_f<=FLOAT_DIV)
							{
								return F_TRUE;
							}
						}
						else{
							if(value_f-value_buf_f<=FLOAT_DIV)
							{
								return F_TRUE;
							}
						}
						return F_FALSE;
//								break;
				}
			break;
			case F_NOT:
			switch(kind)
			{
				case C_LJ_DP_BYTE:
				case C_LJ_DP_UINT16:
				case C_LJ_DP_UINT32:
					if(value_buf!=value)
					{
							return F_TRUE;
					}
					else{
							return F_FALSE;
					}
//									break;
				case C_LJ_DP_INT8:
				case C_LJ_DP_INT16:
				case C_LJ_DP_INT32:
					if(value_buf_i!=value_i)
					{
							return F_TRUE;
					}
					else{
							return F_FALSE;
					}
//									break;
				case C_LJ_DP_FLOAT:
					if(value_buf_f>value_f)
					{
						if(value_buf_f-value_f>FLOAT_DIV)
						{
							return F_FALSE;
						}
					}
					else
					{
						if(value_f-value_buf_f>FLOAT_DIV)
						{
							return F_FALSE;
						}
					}
					return F_TRUE;
//									break;
			}
			break;
			case F_SMALLER_EQUAL:
				switch(kind)
				{
					case C_LJ_DP_BYTE:
					case C_LJ_DP_UINT16:
					case C_LJ_DP_UINT32:
						if(value_buf<=value)
						{
								return F_TRUE;
						}
						else{
								return F_FALSE;
						}
//									break;
					case C_LJ_DP_INT8:
					case C_LJ_DP_INT16:
					case C_LJ_DP_INT32:
						if(value_buf_i<=value_i)
						{
								return F_TRUE;
						}
						else{
								return F_FALSE;
						}
//									break;
					case C_LJ_DP_FLOAT:
						if(value_buf_f>value_f)
						{
							if(value_buf_f-value_f>FLOAT_DIV)
							{
								return F_FALSE;
							}
						}
						return F_TRUE;
//									break;
				}
				break;
			case F_BIGGER_EQUAL:
				switch(kind)
				{
					case C_LJ_DP_BYTE:
					case C_LJ_DP_UINT16:
					case C_LJ_DP_UINT32:
						if(value_buf>=value)
						{
								return F_TRUE;
						}
						else{
								return F_FALSE;
						}
//									break;
					case C_LJ_DP_INT8:
					case C_LJ_DP_INT16:
					case C_LJ_DP_INT32:
						if(value_buf_i>=value_i)
						{
								return F_TRUE;
						}
						else{
								return F_FALSE;
						}
//									break;
					case C_LJ_DP_FLOAT:
						if(value_buf_f<value_f)
						{
							if(value_buf_f-value_f>FLOAT_DIV)
							{
								return F_FALSE;
							}
						}
						return F_TRUE;
//									break;
				}
				break;
		}//switch(maths)

	}


	index=point_find_char_num(buf[0]);
	if(index==0xff)
	{
		// log_zzc("get_true_false index not char index=%02x\r\n",buf[0]);
	}
	else
	{
		// log_zzc("get_true_false index is char index=%02x\r\n",buf[0]);
		if((maths<F_EQUAL)||(maths>F_NOT))
		{
			log_zzc("wrong7\r\n");
			return F_WRONG;
		}
		len=point_char[index].len;
		for(j=i;j<POINT_CHAR_LEN_MAX;j++)
		{
			if(buf[j+2]==0)
			{
				break;
			}
		}
		*len_out=j+1;
		if(j+1<len)
		{
			len=j+1;
		}
		// log_zzc("len=%d,len_out=%d\r\n",len,*len_out);
		kind=point_char[index].kind;
		switch(maths)
		{
		case F_EQUAL:
			for(j=0;j<len;j++)
			{
				if(buf[j+2]!=point_char[index].value[j])
				{
					log_zzc("buf[j+2]=%02x,point_char[index].value[j]=%02x\r\n",buf[j+2],point_char[index].value[j]);
					return F_FALSE;
				}
			}
			return F_TRUE;
			break;
		case F_NOT:
			for(j=0;j<len;j++)
			{
				if(buf[j+2]!=point_char[index].value[j])
				{
					return F_TRUE;
				}
			}
			return F_FALSE ;
			break;
		}
	}

    log_zzc("wrong8\r\n");
    return F_WRONG;

}

void print_buf(unsigned char *buf,unsigned char len)
{
    unsigned int i=0;
    // log_zzc("\r\n");
	log_zzc(" ");
    for(i=0;i<len;i++)
    {
        log_zzc_n("%02x ",buf[i]);
    }
    log_zzc_n("\r\n");
}


void print_stack(unsigned char push)
{
   if(push)
   {
       log_zzc("pushing\r\n");
   }
   else{
       log_zzc("poping\r\n");
   }
   log_zzc("stack_count=%d\r\n",stack_count);
   log_zzc("judge_stack:");
   print_buf(stack_judge_buf,stack_MAX);
   log_zzc("value_stack:");
   print_buf(stack_value_buf,stack_MAX);

}

void push_pop_init(void)
{
	stack_count=0;
}


unsigned char push(unsigned char value,unsigned char judge )
{
    if(stack_count<stack_MAX)
    {
        stack_value_buf[stack_count]=value;
        stack_judge_buf[stack_count]=judge;
        stack_count++;
    //    print_stack(1);
        return 1;
    }
    return 0;
}

unsigned char pop(unsigned char * value,unsigned char *judge )
{
    if(stack_count>0)
    {
        stack_count--;
        *value=stack_value_buf[stack_count];
        *judge=stack_judge_buf[stack_count];

    //    print_stack(0);
        return 1;
    }
    return 0;
}


unsigned char  point_find_kind_len(unsigned char index,unsigned char *kind,unsigned short * len)
{
    unsigned char count=0;
    count= point_find_int_num(index);
    if(count==0xff)
    {

    }
    else
    {
    	*kind=point_int[count].kind;
    	*len=point_int[count].len;
    	return 1;
    }

    count= point_find_char_num(index);
    if(count==0xff)
	{

	}
	else
	{
		*kind=point_char[count].kind;
		*len=POINT_CHAR_LEN_MAX;
		return 1;
	}
    return 0;
}





//#define POINT_INDEX_MAX (0x23)
//
//#define POINT_GUO_INDEX_MIN (0x21)
//#define POINT_GUO_INDEX_MAX (0x23)
//
//#define ALL_POINT_NUM (17)
//#define DIAN_POINT_NUM (11)
//#define GUO_POINT_NUM (3)
//
//#define ALL_NUM_86 (16)
//#define DIAN_NUM_86 (13)
//#define GUO_NUM_86 (6)
//
//#define ALL_BUF_LEN 128
//unsigned char const dian_all_map[DIAN_NUM_86]={0x0f,0x64,0x63,0,1,2,3,4,5,6,7,0x10,P_TIMER};
//// unsigned char all_kind[NUM_86]={0,0,0,0,0,0,0};
//unsigned char const dian_all_len[DIAN_NUM_86]={1,5,1,1,1,1,1,1,1,2,1,1,2};
//
//unsigned char const guo_all_map[GUO_NUM_86]={0x0f,0x64,0x63,0x21,0x22,0x23};
//// unsigned char all_kind[NUM_86]={0,0,0,0,0,0,0};
//unsigned char const guo_all_len[GUO_NUM_86]={1,5,1,1,1,1};
//
//unsigned char const  all_all_map[ALL_NUM_86]={0x0f,0x64,0x63,0,1,2,3,4,5,6,7,0x10,0x21,0x22,0x23,P_TIMER};
//// unsigned char all_kind[NUM_86]={0,0,0,0,0,0,0};
//unsigned char const all_all_len[ALL_NUM_86]={1,5,1,1,1,1,1,1,1,2,1,1,1,1,1,2};
//
//
//#define DIAN_MCU_NUM_86 (10)
//#define GUO_MCU_NUM_86 (4)
//
//unsigned char const guo_mcu_all_map[GUO_MCU_NUM_86]={0x0f,0x21,0x22,0x23};
//// unsigned char all_kind[NUM_86]={0,0,0,0,0,0,0};
//unsigned char const guo_mcu_all_len[GUO_MCU_NUM_86]={1,1,1,1};
//
//unsigned char const dian_mcu_all_map[DIAN_MCU_NUM_86]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x63,0x10};
//// unsigned char all_kind[NUM_86]={0,0,0,0,0,0,0};
//unsigned char const dian_mcu_all_len[DIAN_MCU_NUM_86]={1,1,1,1,1,1,2,1,1,1};



//#ifdef _up_add_kind_
//#define POINT_NUM (11)
//#define NUM_86 (6)
//#define ALL_BUF_LEN 128
//unsigned char all_map[NUM_86]={0,1,2,3,5,6};
//// unsigned char all_kind[NUM_86]={0,0,0,0,0,0,0};
//unsigned char all_len[NUM_86]={1,1,1,1,1,1}; 
//#else

//#define POINT_NUM (11)
//#define NUM_86 (7)
//#define ALL_BUF_LEN 128
//unsigned char all_map[NUM_86]={0x0f,0,1,2,3,5,6};
//// unsigned char all_kind[NUM_86]={0,0,0,0,0,0,0};
//unsigned char all_len[NUM_86]={1,1,1,1,1,1,1};  
//#endif //_up_add_kind_
/////////////////////////////////////////////////////
void print_aci(unsigned char *buf,unsigned char len);





//bool point_is_guo(unsigned char index)
//{
//	if((index>=POINT_GUO_INDEX_MIN)&&(index<=POINT_GUO_INDEX_MAX))
//	{
//		return true;
//	}
//	return false;
//}











unsigned char  check_condition(unsigned char *buf_in,unsigned char len)
{
    unsigned short i=0,j=0;
//    unsigned char get_left=0;
//    unsigned char get_judge=0;
//    unsigned char res=0;
    unsigned char add_buf=0;
    unsigned char status_buf=0;
    unsigned short point_len=0;
    unsigned char res_buf=F_TRUE;
    unsigned char judge_buf=F_EMPTY;
    unsigned char res_buf_buf=0;
    unsigned char judge_buf_buf=F_EMPTY;
	
		unsigned char buf[JUDGE_BUF_MAX]={0};
		if(len>JUDGE_BUF_MAX)
		{
			log_zzc_error("check_condition len is too big\r\n");
			return 0xff;
		}
		for(i=0;i<len;i++)
		{
			buf[i]=buf_in[i];
		}
    for(i=0;i<len;)//??,????,????????????,?????????????????empty
    {
        if(buf[i]<F_END)
        {
            add_buf=i;
            status_buf=get_true_false(&buf[add_buf],&point_len);
            if(status_buf==F_WRONG)
            {
                log_zzc("wrong1\r\n");
                return 0xff;
            }
            buf[add_buf]=status_buf;
            for(j=0;j<point_len+1;j++)
            {
                buf[add_buf+j+1]=F_EMPTY;
            }
            i=i+point_len+2;
        }
        else{
            i++;
        }
    }
    print_buf(buf,len);

	push_pop_init();
		
    for(i=0;i<len;i++)
    {
        if((buf[i]>=F_AND)&&(buf[i]<=F_OR))
        {
            judge_buf=buf[i];
        }
        else if (buf[i]==F_LEFT)
        {
            if(judge_buf==F_EMPTY)
            {
                if(push(res_buf,F_EMPTY)==0)
                {
                    log_zzc("wrong2\r\n");
                    return 0xff ;
                }
            }
            else{
                if(push(res_buf,judge_buf)==0)
                {
                    log_zzc("wrong3\r\n");
                    return 0xff;
                }
            }
            res_buf=0;
            judge_buf=F_EMPTY;

        }
        else if(buf[i]==F_RIGHT)
        {
			// log_zzc("get int to pop\r\n");
            if(pop(&res_buf_buf,&judge_buf_buf)==0)
            {
                log_zzc("wrong6\r\n");
                return 0xff;
            }
            if(judge_buf_buf==F_EMPTY)
            {
                
            }
            else{
                judge_buf=judge_buf_buf;
                if(judge_buf==F_AND)
                {
                    if((res_buf==F_TRUE)&&(res_buf_buf==F_TRUE))
                    {
                        res_buf=F_TRUE;
                    }
                    else{
                        res_buf=F_FALSE;
                    }
                }
                else if(judge_buf==F_OR)
                {
                    if((res_buf==F_TRUE)||(res_buf_buf==F_TRUE))
                    {
                        res_buf=F_TRUE;
                    }
                    else{
                        res_buf=F_FALSE;
                    }
                }
                else{
                    log_zzc("wrong4\r\n");
                    return 0xff;
                }
                judge_buf=F_EMPTY;
            }
        }
        else if((buf[i]==F_TRUE)||(buf[i]==F_FALSE))
        {
            if(judge_buf==F_EMPTY)
            {
                res_buf=buf[i];
            }
            else{
                if(judge_buf==F_AND)
                {
                    if((res_buf==F_TRUE)&&(buf[i]==F_TRUE))
                    {
                        res_buf=F_TRUE;
                    }
                    else{
                        res_buf=F_FALSE;
                    }
                }
                else if(judge_buf==F_OR)
                {
                    if((res_buf==F_TRUE)||(buf[i]==F_TRUE))
                    {
                        res_buf=F_TRUE;
                    }
                    else{
                        res_buf=F_FALSE;
                    }
                }
                else{
                    log_zzc("wrong5\r\n");
                    return 0xff;
                }
                judge_buf=F_EMPTY;
            }
        }
    }
	if(res_buf==F_TRUE)
   	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void print_aci(unsigned char *buf,unsigned char len)
{
    unsigned char i=0;
	log_zzc(" ");
    log_zzc_n("test_buf=");
    for(i=0;i<len;i++)
    {
        switch(buf[i])
        {
            case F_AND:
            log_zzc_n("&& ");
            break;
            case F_OR:
            log_zzc_n("|| ");
            break;
            case F_BIGGER:
            log_zzc_n("> ");
            break;
            case F_SMALLER:
            log_zzc_n("< ");
            break;
            case F_EQUAL:
            log_zzc_n("= ");
            break;
            case F_NOT:
            log_zzc_n("!= ");
            break;
            case F_LEFT:
            log_zzc_n("( ");
            break;
            case F_RIGHT:
            log_zzc_n(") ");
            break;
            default:
            log_zzc_n("%02x ",buf[i]);
            break;
        }
    }
    log_zzc_n("\r\n");
}




//#include "lj_proto.h"

// point_find_kind_len

// #define CMD_SINGLE
unsigned char cmd_buf[CMD_BUF_MAX]={0};
unsigned char judge_buf[JUDGE_BUF_MAX]={0};
unsigned short judge_len=0;

// return :0xff =wrong 0=fault 1=success 2=success and need jump
unsigned char caipu_data_deal(unsigned char *buf ,unsigned short buf_len,unsigned char *need_jump_to)
{
    unsigned short i=0,j=0;
    // unsigned short cmd_len=0;
    unsigned char index=0;
    unsigned char kind=0;
    unsigned short len=0;
    unsigned short cmd_count=0;
    unsigned short judge_len_buf=0;
	unsigned short yes_len=0;
	unsigned short no_len=0;
	unsigned char judge_ret=0;
	// static unsigned char judge_ret_old=0;
	unsigned char jump_to_index=0;
	unsigned char get_jump=0;
	unsigned char cmd=0;
	unsigned short now_len_buf=0;
	unsigned short now_cmd_len=0;
		// log_zzc("get in to caipu_data_deal,len=%d\r\n",buf_len);
    if(buf_len<4)
    {
		log_zzc_error("buf_len is small than 4\r\n");
        return 0xff;
    }

    caipu_buzhou_show=buf[0];

		judge_len_buf=buf[1];
		
//    cmd_len=buf[0];
		if(judge_len_buf>(buf_len-4))
		{
			log_zzc_error("judge_len_buf is bigger than buf_len-4\r\n");
			return 0xff;
		}
		yes_len=buf[2];
		if(yes_len>(buf_len-3))
		{
			log_zzc_error("yes_len is bigger than buf_len-3\r\n");
			return 0xff;
		}
		if((judge_len_buf+yes_len)>buf_len-3)
		{
			log_zzc_error("judge_len_buf + yes_len is bigger than buf_len-3\r\n");
			return 0xff;
		}
		no_len=buf_len-3-judge_len_buf-yes_len;
		if(no_len>(buf_len-3))
		{
			log_zzc_error("no_len is bigger than buf_len-3\r\n");
			return 0xff;
		}
		if((judge_len_buf+no_len)>buf_len-3)
		{
			log_zzc_error("judge_len_buf + no_len is bigger than buf_len-3\r\n");
			return 0xff;
		}
	// 	cmd_len=buf_len-judge_len_buf-1;
    // if((cmd_len>CMD_LEN_MAX )||(cmd_len>(buf_len-1)))
    // {
	// 	log_zzc_error("cmd_len is to big\r\n");
    //     return 0xff;
    // }
//    judge_len_buf=buf_len-cmd_len-1;

    #ifndef CMD_SINGLE 
		if(caipu_u_send_buf!=NULL)
		{
			caipu_u_send_buf(cmd_buf,cmd_count,c_send_point);
		}
//    lj_set_point(cmd_buf,cmd_count);
    #endif//CMD_SINGLE
    judge_delay_time=JUDGE_DELAY_TIME;
    for(i=0;i<judge_len_buf;i++)
    {
        judge_buf[i]=buf[i+3];
    }
    judge_len=judge_len_buf;

	judge_ret=check_condition(judge_buf,judge_len);


	if(judge_ret==0xff)
	{
		log_zzc_error("check_condition return wrong\r\n");
		return 0xff;
	}
	else if(judge_ret==1){
		now_len_buf=judge_len_buf+4;
		now_cmd_len=yes_len;
		log_zzc("judge_ret return 1,yes_len =%d",yes_len);
	}
	else{
		now_len_buf=judge_len_buf+4+yes_len;
		now_cmd_len=no_len;
		log_zzc("judge_ret return 0,no_len =%d",no_len);
	}

		cmd=buf[now_len_buf-1];
		log_zzc("yes_len=%d,now_len_buf=%d,cmd =%d,now_cmd_len=%d\r\n",yes_len,now_len_buf,cmd,now_cmd_len);

		for(i=0;i<now_cmd_len-1;)
		{
//			cmd=buf[i+now_len_buf];
			
		
			if(cmd==CMD_JUMP)//F_JUMP
			{
				// index=F_JUMP;
				jump_to_index=buf[i+now_len_buf];
				if((jump_to_index>caipu_buzhou_num)||(jump_to_index<1))
				{
					log_zzc_error("jump_to_index is bigger than caipu_buzhou_num or smaller than 1\r\n");
					return 0xff;
				}
				else{
					*need_jump_to=jump_to_index-1;
					get_jump=1;
				}
				len=1;
				i=i+len;
			}
			else if(cmd==CMD_RUN)
			{
				index=buf[i+now_len_buf];

				if(point_find_kind_len(index,&kind,&len)==0)
				{
					log_zzc_error("can not find index\r\n");
					return 0xff;

				}
				if(len>now_cmd_len-i)
				{

					len=now_cmd_len-i;
					log_zzc("cmd len too big and cut");
				}
				if(cmd_count+3+len>CMD_BUF_MAX)
				{
					log_zzc_error("no enough buffer for next cmd\r\n");
					return 0xff;
				}
				#ifdef _dianfanguo_
				cmd_buf[cmd_count]=0x04;cmd_count++;
				cmd_buf[cmd_count]=0x40;cmd_count++;
				cmd_buf[cmd_count]=0x04;cmd_count++;
				cmd_buf[cmd_count]=caipu_head[0];cmd_count++;
				cmd_buf[cmd_count]=caipu_head[1];cmd_count++;
				cmd_buf[cmd_count]=caipu_head[2];cmd_count++;
				cmd_buf[cmd_count]=caipu_head[3];cmd_count++;

				#endif //_dianfanguo_
				cmd_buf[cmd_count]=index;
				cmd_count++;
				cmd_buf[cmd_count]=(kind&0xf0)+((len>>8)&0x0f);
				cmd_count++;
				cmd_buf[cmd_count]=len;
				cmd_count++;
				for(j=0;j<len;j++)
				{
					cmd_buf[cmd_count+j]=buf[i+j+2+now_len_buf];

				}

				#ifdef CMD_SINGLE
				if(caipu_send_buf_fun!=NULL)
				{
						
						cmd_count=cmd_count+len;
						log_zzc("cmd_count=%d,cmd_buf= %02x %02x %02x %02x\r\n",cmd_count,cmd_buf[0],cmd_buf[1],cmd_buf[2],cmd_buf[3]);
						caipu_send_buf_fun(cmd_buf,cmd_count);

				}
				else
				{
					log_zzc("caipu_send_buf_fun is NULL");
				}
		//        lj_set_point(cmd_buf,cmd_count);
				cmd_count=0;
				#endif// CMD_SINGLE
				i=i+len+1;
			}
			else if(cmd==CMD_SLEEP) 
			{
				judge_delay_time=buf[i+now_len_buf+1]*256*256*256+buf[i+now_len_buf+2]*256*256+buf[i+now_len_buf+3]*256+buf[i+now_len_buf+4];
				log_zzc("run to  CMD_SLEEP,judge_delay_time=%d\r\n",judge_delay_time);
				len=4;
				i=i+len;
			}
			else{
//				len=1;
//				i=i+len+1;
				log_zzc("run caipu cmd wrong");
				return 0xff;
			}
			
		}
		if(get_jump)
		{
			if(judge_ret==1)
			{
				return 3;
			}
			else
			{
				return 2;
			}
		}
		else {
			if(judge_ret==1){
				return 1;
			}
			else{
				return 0;
			}
		}

}

void judge_time_loop(void)//s
{

    if (judge_delay_time)
    {
        judge_delay_time--;
    }
		if(caipu_timer_delay>0)
		{
			caipu_timer_delay--;
		}
    // return check_condition(judge_buf,judge_len);
}




unsigned char caipu_buzhou_data[CAIPU_BUZHOU_COUNT_MAX][CAIPU_BUF_MAX]={0};
unsigned char caipu_buzhou_data_len[CAIPU_BUZHOU_COUNT_MAX]={0};

void empty_caipu(void)
{
    unsigned char i=0;
    for(i=0;i<CAIPU_BUZHOU_COUNT_MAX;i++)
    {
        caipu_buzhou_data_len[i]=0;
    }
}

unsigned char  caipu_u_deal_64(unsigned char *buf,unsigned char len)
{
		unsigned short cmd_count=0;
		log_zzc("get in to caipu_u_deal_64,len=%d\r\n",len);
    if(len!=6)
    {
        return 0 ;
    }
    caipu_version=buf[0];
    caipu_code[0]=buf[1];
    caipu_code[1]=buf[2];
    caipu_code[2]=buf[3];
    caipu_code[3]=buf[4];
    caipu_buzhou_num=buf[5];


    if(caipu_version!=CAIPU_VERSION)
    {
    	log_zzc_error("caipu_version set = %d, but is must be %d\r\n",caipu_version,CAIPU_VERSION);
    	return 0;
    }

	if(caipu_buzhou_num==0)
	{
		log_zzc_error("caipu_buzhou_num is 0 and can start caipu\r\n");
		return 0;
	}
		caipu_head[0]=caipu_version;
		caipu_head[1]=caipu_code[0];
		caipu_head[2]=caipu_code[1];
		caipu_head[3]=caipu_code[2];
		caipu_head[4]=caipu_code[3];
		caipu_head[5]=caipu_buzhou_num;
    empty_caipu();
		// liandong_gonglv=0;
    caipu_heartbeat_timeout=0;
    caipu_heartbeat_time=0;
    caipu_step=1;
    caipu_buzhou_now=0;
//    	if(caipu_send_buf_fun!=NULL)
//    	{
//    		caipu_send_buf_fun(cmd_buf,7);
//    	}

#ifdef _CAIPU_STD_
		cmd_buf[0]=U_BEGIN;
		cmd_buf[1]=0x00;
		cmd_buf[2]=0x01;
		cmd_buf[3]=0x01;
		cmd_count=4;
		caipu_send_buf_fun(cmd_buf,cmd_count);

		cmd_buf[0]=U_HEAD;
		cmd_buf[1]=0xA0;
		cmd_buf[2]=0x06;
		cmd_buf[3]=caipu_head[0];
		cmd_buf[4]=caipu_head[1];
		cmd_buf[5]=caipu_head[2];
		cmd_buf[6]=caipu_head[3];
		cmd_buf[7]=caipu_head[4];
		cmd_buf[8]=caipu_head[5];
		cmd_count=9;

		caipu_send_buf_fun(cmd_buf,cmd_count);

		// cmd_buf[0]=U_STEP;
		// cmd_buf[1]=0X00;
		// cmd_buf[2]=0x01;
		// cmd_buf[3]=0x01;
		// cmd_count=4;
		// caipu_send_buf_fun(cmd_buf,cmd_count);


#endif//_CAIPU_STD_
		caipu_change=1;
		return 1;
}

void caipu_u_deal_65(unsigned char index,unsigned char *buf,unsigned short len)
{
    unsigned short i=0;
		log_zzc("get in to caipu_u_deal_65,index=%d,len=%d\r\n",index,len);
    if((index<0x65)||(index>200))
    {
		log_zzc_error("error in deal_65 index si not right\r\n");
        return;
    }
    if(len>CAIPU_BUF_MAX)
    {
    	log_zzc_error("caipu deal_65 len is bigger than CAIPU_BUF_MAX");
    }
    if((index-0x65)>=CAIPU_BUZHOU_COUNT_MAX)
    {
    	log_zzc_error("caipu deal_65 num is bigger than CAIPU_BUZHOU_COUNT_MAX");
    }
    for(i=0;i<len;i++)
    {
        caipu_buzhou_data[index-0x65][i]=buf[i];
    }
    caipu_buzhou_data_len[index-0x65]=len;
	print_aci(caipu_buzhou_data[index-0x65],caipu_buzhou_data_len[index-0x65]);
}


unsigned char caipu_is_running(void)
{
	if(caipu_step)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

static void caipu_end(void)
{
	unsigned short cmd_count=0;
	// liandong_gonglv=0;
    caipu_step=0;
    caipu_buzhou_now=0;

#ifdef _CAIPU_STD_
		cmd_buf[0]=0x63;
		cmd_buf[1]=0x00;
		cmd_buf[2]=0x01;
		cmd_buf[3]=0x00;
		cmd_count=4;
		caipu_send_buf_fun(cmd_buf,cmd_count);
#endif //_CAIPU_STD_
	
	caipu_head[0]=0;
	caipu_head[1]=0;
	caipu_head[2]=0;
	caipu_head[3]=0;
	caipu_head[4]=0;
	caipu_buzhou_num=0;
	
	caipu_change=1;
}

unsigned char caipu_step_old=0xff;

void caipu_loop(void)
{
    unsigned char deal_ret=0;
	// static unsigned char judge_ret_old=0;
//	unsigned char cmd_count=0;
	unsigned char send_buf[4]={0};
	unsigned char need_jump_to=0;
	// if(caipu_step!=caipu_step_old)
	// {
	// 	log_zzc("caipu_step=%d\r\n",caipu_step);
	// }
	// caipu_step_old=caipu_step;
	if(need_end_caipu)
	{
		need_end_caipu=0;
		log_zzc("need caipu end---------------\r\n");
		caipu_end();
	}
	
    switch (caipu_step)
    {
        case 0:
        break;
        case 1:
        if(caipu_buzhou_now<caipu_buzhou_num)
        {
#ifdef _CAIPU_STD_
			send_buf[0]=U_STEP;
			send_buf[1]=0x00;
			send_buf[2]=0x01;
			send_buf[3]=caipu_buzhou_now+1;
			caipu_send_buf_fun(send_buf,4);
#endif //_CAIPU_STD_
			deal_ret=caipu_data_deal(caipu_buzhou_data[caipu_buzhou_now],caipu_buzhou_data_len[caipu_buzhou_now],&need_jump_to);
            if(deal_ret==0xff)
			{
				caipu_step=100;
			}
			else{
				if(deal_ret==3)
				{
					log_zzc("judge [%d] of [%d] is pass and jump to %d \r\n",caipu_buzhou_now+1,caipu_buzhou_num,need_jump_to+1);
					caipu_buzhou_now=need_jump_to;
					caipu_change=1;
// #ifdef _CAIPU_STD_
// 					send_buf[0]=U_STEP;
// 					send_buf[1]=0x00;
// 					send_buf[2]=0x01;
// 					send_buf[3]=caipu_buzhou_now+1;
// 					caipu_send_buf_fun(send_buf,4);
// #endif //_CAIPU_STD_
					
				}
				else if(deal_ret==2)
				{
					log_zzc("judge [%d] of [%d] is not pass and jump to %d \r\n",caipu_buzhou_now+1,caipu_buzhou_num,need_jump_to+1);
					caipu_buzhou_now=need_jump_to;
					caipu_change=1;
// #ifdef _CAIPU_STD_
// 					send_buf[0]=U_STEP;
// 					send_buf[1]=0x00;
// 					send_buf[2]=0x01;
// 					send_buf[3]=caipu_buzhou_now+1;
// 					caipu_send_buf_fun(send_buf,4);
// #endif //_CAIPU_STD_
					
				}
				else if(deal_ret==1)
				{
					log_zzc("judge [%d] of [%d] is pass\r\n",caipu_buzhou_now+1,caipu_buzhou_num);
					if(caipu_buzhou_now<caipu_buzhou_num)
					{
						caipu_buzhou_now++;
						caipu_change=1;
						if(caipu_buzhou_now==caipu_buzhou_num)
						{
							caipu_step=100;
							break;
						}

					// caipu_timer_delay=0;
// #ifdef _CAIPU_STD_
// 					send_buf[0]=U_STEP;
// 					send_buf[1]=0x00;
// 					send_buf[2]=0x01;
// 					send_buf[3]=caipu_buzhou_now+1;
// 					caipu_send_buf_fun(send_buf,4);
// #endif //_CAIPU_STD_
					}
					
				}
				else{
					log_zzc("judge [%d] of [%d] is not pass\r\n",caipu_buzhou_now+1,caipu_buzhou_num);
					if(caipu_buzhou_now<caipu_buzhou_num)
					{
						caipu_buzhou_now++;
						caipu_change=1;
						if(caipu_buzhou_now==caipu_buzhou_num)
						{
							caipu_step=100;
							break;
						}
// #ifdef _CAIPU_STD_
// 						send_buf[0]=U_STEP;
// 						send_buf[1]=0x00;
// 						send_buf[2]=0x01;
// 						send_buf[3]=caipu_buzhou_now+1;
// 						caipu_send_buf_fun(send_buf,4);
// #endif //_CAIPU_STD_
					}
					
				}

            	caipu_step++;

				// judge_delay_time=JUDGE_DELAY_TIME_1;
			}
        }
        else{
            caipu_step=100;
					
        }
        break;
        case 100:
				log_zzc("caipu end---------------\r\n");
        caipu_end();
        break;
        case 2:
#ifdef _CAIPU_STD_
        caipu_heartbeat_time++;//100ms
        if(caipu_heartbeat_time>=10*10)
        {
        	caipu_heartbeat_time=0;
        	caipu_heartbeat_timeout++;
        	log_zzc("caipu_heartbeat_timeout=%d",caipu_heartbeat_timeout);
        	if(caipu_heartbeat_timeout>3)
        	{
        		log_zzc("caipu_heartbeat_timeout---------------\r\n");
        		caipu_step=100;
        	}
        	else
        	{
				send_buf[0]=U_HEARTBEAT;
				send_buf[1]=0x00;
				send_buf[2]=0x01;
				send_buf[3]=1;
				caipu_send_buf_fun(send_buf,4);
        	}
        }
#endif //_CAIPU_STD_
        if(judge_delay_time==0)
        {
        	log_zzc("judge_delay_time=0\r\n");
            // judge_ret=check_condition(judge_buf,judge_len);
            // if(judge_ret==1)
            // {
            	caipu_step=1;
            // }
			// else if(judge_ret==0xff)
			// {
			// 	log_zzc_error("check_condition return wrong\r\n");
			// 	caipu_step=100;
			// }
			// else{
			// 	if(judge_ret!=judge_ret_old)
			// 	{
			// 		log_zzc("judge_ret_old=%d",judge_ret_old);
			// 	}
			// 	judge_ret_old=judge_ret;
			// }
        }
        break;

    }

}




//unsigned char  caipu_guo_refresh_value_86(unsigned char *buf,unsigned short len)
//{
//    unsigned short i=0,count=0;
//	unsigned char kind_buf=dev_kind;
//	if(len<4)
//	{
//		return 0 ;
//	}
//    for(i=0;i<GUO_MCU_NUM_86;i++)
//    {
//				if(guo_mcu_all_map[i]==0x0f)//when set kind, it use | then  kind will never reset by u_deal if it connecting to diancilu
//				{
//					kind_buf|=buf[count];
//					set_point_value(guo_mcu_all_map[i],&kind_buf);
//					printf("dev_kind change to %02x %d",dev_kind,__LINE__);
//				}
//				else
//				{
//        set_point_value(guo_mcu_all_map[i],&buf[count]);
//
//				}
//				count=count+guo_mcu_all_len[i];
//    }
//		print_point();
//    return 1;
//}
//
//unsigned char  caipu_dian_refresh_value_86(unsigned char *buf,unsigned short len)
//{
//    unsigned short i=0,count=0;
//	unsigned char kind_buf=dev_kind;
//	if(len<4)
//	{
//		return 0 ;
//	}
//    for(i=0;i<DIAN_MCU_NUM_86;i++)
//    {
//				if(dian_mcu_all_map[i]==0x0f)//when set kind, it use | then  kind will never reset by u_deal if it connecting to diancilu
//				{
//					kind_buf|=buf[count];
//					set_point_value(dian_mcu_all_map[i],&kind_buf);
//					printf("dev_kind change to %02x %d",dev_kind,__LINE__);
//				}
//				else if(dian_mcu_all_map[i]==0x63)
//				{
//					if((caipu_step)&&(buf[count]==0))
//					{
//						caipu_end();
//					}
//				}
//				else
//				{
//        set_point_value(dian_mcu_all_map[i],&buf[count]);
//
//				}
//				count=count+dian_mcu_all_len[i];
//    }
//		print_point();
//    return 1;
//}

//unsigned char  caipu_dian_refresh_value_86(unsigned char *buf,unsigned short len)
//{
//    unsigned short i=0,count=0;
//	unsigned char kind_buf=dev_kind;
//	if(len<4)
//	{
//		return 0 ;
//	}
//    for(i=0;i<GUO_MCU_NUM_86;i++)
//    {
//				if(guo_mcu_all_map[i]==0x0f)//when set kind, it use | then  kind will never reset by u_deal if it connecting to diancilu 
//				{
//					kind_buf|=buf[count];
//					set_point_value(guo_mcu_all_map[i],&kind_buf);
//					printf("dev_kind change to %02x %d",dev_kind,__LINE__);
//				}
//				else
//				{
//        set_point_value(guo_mcu_all_map[i],&buf[count]);
//        
//				}
//				count=count+guo_mcu_all_len[i];
//    }
//		print_point();
//    return 1;
//}






void caipu_init(caipu_send_buf_fun_t send_buf_fun )
{
	caipu_send_buf_fun=send_buf_fun;
}

unsigned char  caipu_point_deal(unsigned char *buf,unsigned short len)
{
	unsigned short i=0;
	unsigned char index=0;
	unsigned short p_len=0;
	unsigned char ret=0;
	for(i=0;i<len;)
	{
		index=buf[i];
		p_len=(buf[i+1]&0x0f)*256+buf[i+2];
		if(index==F_CAIPU_INDEX)
		{
			if(caipu_u_deal_64(&buf[i+3],p_len)==0)
			{
				ret=0;
				return ret;
			}
			else
			{
				ret=1;
			}
		}
		else if((index<=F_CAIPU_MAX)&&(index>=F_CAIPU_FIRST))
		{
			if(caipu_version!=CAIPU_VERSION)
			{
				need_end_caipu=1;
				return 0;
			}
			caipu_u_deal_65(index,&buf[i+3],p_len);
			ret=1;
		}
		else
		{
			need_end_caipu=1;
			log_zzc("index is not belong to caipu need end caipu");
		}
		i=i+p_len+3;
	}
	return ret;
}


unsigned int time_100ms=0;


void caipu_loop_fun(void)//100ms
{
	time_100ms++;
	if(time_100ms>=10)
	{
		time_100ms=0;
		judge_time_loop();
	}
	// judge_delay_time=0;//02040322
	caipu_loop();
//	if(caipu_change)
//	{
//		caipu_change=0;
////		air_send_caipu_status();
//	}

}


unsigned char  get_caipu_change(void)
{
	return caipu_change;
}

void reset_caipu_change(void)
{
	caipu_change=0;
}

void set_caipu_change(void)
{
	caipu_change=1;
}

//#define CAIPU_WIFI_PIP (0xf0)
//#define CAIPU_WIFI_SET_UPDATE (0XFC)
//#define CAIPU_WIFI_END (0xfb)
//#define CAIPU_WIFI_STEP (0XFA)
//#define CAIPU_WIFI_MENU_ID (0XF9)



void caipu_change_buf_fill(unsigned char *buf,unsigned char *len)
{
	if((buf==NULL)||(*len<11))
	{
		return ;
	}
	buf[0]=CAIPU_WIFI_MENU_ID;
	buf[1]=0x40;
	buf[2]=0x04;
	buf[3]=caipu_code[0];
	buf[4]=caipu_code[1];
	buf[5]=caipu_code[2];
	buf[6]=caipu_code[3];

	buf[7]=CAIPU_WIFI_STEP;
	buf[8]=0x00;
	buf[9]=0x01;
	if(caipu_buzhou_num==0)
	{
		buf[10]=0;
	}
	else
	{
		buf[10]=caipu_buzhou_show;
	}
	*len=11;
}

void need_end_caipu_fun(void)
{
	need_end_caipu=1;
}

/* test

// unsigned char test_buf[]={0x64,0x90,0x05,0x01,0x02,0x03,0x04,0x05};
// unsigned char test_buf[]={0x65,0x90,0x16,0x04,0x00,0x00,0x01,0x00,0x00,0xca,0x01,0xc8,0x01,0xcb,0x01,0xc9,0xce,0x02,0xca,0x02,0xc8,0x03,0xcb,0x02,0xcf};
// unsigned char test_buf[]={0x64,0x90,0x05,0x01,0x02,0x03,0x04,0x05,0x65,0x90,0x16,0x04,0x00,0x00,0x01,0x00,0x00,0xca,0x01,0xc8,0x01,0xcb,0x01,0xc9,0xce,0x02,0xca,0x02,0xc8,0x03,0xcb,0x02,0xcf};
// unsigned char test_buf[]={0x64,0xa0,0x05,0x01,0x02,0x03,0x04,0x05,0x65,0x90,15,10,F_LEFT,0x02,F_BIGGER,0x00,0X01,F_RIGHT,F_AND,0x01,F_SMALLER,0x01,0x01,0x02,0x00,0x01};
// unsigned char test_buf[]={0x64,0xa0,0x05,0x01,0x02,0x03,0x04,0x01,0x65,0x90,14,8,F_LEFT,0x06,F_NOT,0x31,0X32,0x33,0,F_RIGHT,0x01,0x02,0x02,0x00,0x01};
unsigned char test_buf[]={0x64,0xa0,0x05,0x01,0x02,0x03,0x04,0x04,
0x65,0x90,17,8,7,F_LEFT,0x06,F_NOT,0x31,0X32,0x33,0,F_RIGHT,CMD_RUN,0x01,0x02,CMD_RUN,0x02,0x00,0x01 ,
0x66,0x90,0x08,0x03,0x03,0x07,F_SMALLER,0x02,CMD_RUN,0x03,0x01,
0x67,0x90,0x08,0x03,0x03,0x07,F_BIGGER,0x04,CMD_RUN,0x03,0x00,
0x68,0x90,10,0x03,0x03,0x08,F_EQUAL,0x01,CMD_RUN,0x03,0x02,CMD_JUMP,0x02,
};


unsigned char test_85[]={0x01,0x00,0x01,0x06,0x02,0x00,0x01,0x05};
unsigned char test_86[]={0x06,0x05,0x03,0x04,0x05,0x06,0x07,0x08};

unsigned char test_851[]={0x01,0x00,0x01,0x05,0x02,0x00,0x01,0x05};

unsigned char lj_setting[]={0x02,0x09};
int lj_setting_len=2;

void point_init(void)
{

}


void log_send(unsigned char *buf, unsigned short len)
{
	unsigned short i=0;
	printf("log_send:");
	for(i=0;i<len;i++)
	{
		printf(" %02x",buf[i]);
	}
	printf("\r\n");
}

// #define ARRAY_SIZE 4 // 瀹氫箟鏁扮粍澶у皬
 
    // int arr[ARRAY_SIZE];
    // unsigned char i;
#define MAX_SIZE 100
unsigned char arr[MAX_SIZE];
    int n;


int main(void)
{
	caipu_init(log_send);
	print_point();
	caipu_point_deal(test_buf,sizeof(test_buf));
	// caipu_deal_85(test_85,sizeof(test_85));
	// caipu_deal_86(test_86,sizeof(test_86));
	// reset_change();
	// log_zzc("reset_change\r\n");
	// print_point();
	// caipu_deal_86(test_86,sizeof(test_86));
	// reset_change();
	// log_zzc("reset_change\r\n");
	// caipu_deal_85(test_851,sizeof(test_851));
	// if(check_change(lj_setting,lj_setting_len))
	// {
	// 	log_zzc("had change\r\n");
	// }


	while(1)
	{
		caipu_loop_fun();
		// printf("璇疯緭鍏� %d 涓暣鏁帮細\n", ARRAY_SIZE);
		// for (i = 0; i < ARRAY_SIZE; i++) {
		// 	scanf("%d", &arr[i]);
		// 	// 妫�鏌ユ槸鍚﹁緭鍏ユ垚鍔�
		// 	if (scanf("%d", &arr[i]) != 1) {
		// 		fprintf(stderr, "杈撳叆閿欒锛屼腑鏂緭鍏ャ�俓n");
		// 		break;
		// 	}
		// }
		// // 杈撳嚭鏁扮粍鍐呭楠岃瘉
		// printf("\n鎮ㄨ緭鍏ョ殑鏁扮粍涓猴細");
		// for (i = 0; i < ARRAY_SIZE; i++) {
		// 	printf("%d ", arr[i]);
		// }
		// printf("\n");
		printf("Enter the number of elements in the array: ");
		scanf("%d", &n);

		if (n > MAX_SIZE || n <= 0) {
			printf("Invalid input for array size.\n");
			// return 1;
		}

		printf("Enter %d elements:\n", n);
		for (int i = 0; i < n; i++) {
			scanf("%02x", &arr[i]);
		}

		printf("Entered array elements are: ");
		for (int i = 0; i < n; i++) {
			printf("%02x ", arr[i]);
		}
		printf("\n");

		caipu_deal_85(arr,n);
	}
}

*/

